﻿Module fun
    ' 数据库路径
    Public Const dbPath = "d.db"
    Public dbListCount As Integer = 0
    ' 添加日志条目到 ListView
    Public Sub AddInfo(info As String)
        Dim logTime As String = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
        Dim logItem As New ListViewItem(logTime)
        logItem.SubItems.Add(info)
        With Form1.ListView
            '新增信息
            .Items.Add(logItem)

            ' 选中最后一行
            .Items(.Items.Count - 1).Selected = True

            ' 确保最后一行在视图中可见
            .EnsureVisible(.Items.Count - 1)
        End With

        Application.DoEvents()
    End Sub
    ' 文件选择
    Function PickFile(ByVal FileDialog As System.Windows.Forms.OpenFileDialog _
                  , Optional myType As String = "ex" _
                  , Optional isMulit As Boolean = False _
                  , Optional fd0 As String = "" _
                  , Optional ElseStrF As String = "") As String()
        Dim fname() As String = Nothing
        Dim strFilt As String = ""

        With FileDialog
            If Not String.IsNullOrEmpty(fd0) Then
                .InitialDirectory = fd0
            End If

            Select Case UCase(myType)
                Case "EX"
                    strFilt &= "Excel(*.xls;*.xlsx)|*.xls;*.xlsx" & "|"
                Case "TXT"
                    strFilt &= "Txt files(*.txt;*.csv)|*.txt;*.csv" & "|"
                Case Else
                    If Not String.IsNullOrEmpty(ElseStrF) Then
                        strFilt &= ElseStrF & "|"
                    End If
            End Select

            strFilt &= "All Files(*.*)|*.*"
            .Filter = strFilt
            .Multiselect = isMulit
            .SupportMultiDottedExtensions = True
            If .ShowDialog() = DialogResult.OK Then
                fname = .FileNames
            End If
        End With
        Return fname
    End Function
    ''' <summary>
    ''' 选择文件夹对话框（共享方法版）
    ''' </summary>
    Public Function PickFolder(ByVal dialog As FolderBrowserDialog,
                                      Optional description As String = "选择文件夹",
                                      Optional initialPath As String = "") As String
        ' 设置初始路径
        If String.IsNullOrEmpty(initialPath) Then
            initialPath = Application.StartupPath
        End If

        ' 配置对话框
        With dialog
            .Description = description
            .SelectedPath = initialPath
            .ShowNewFolderButton = True  ' 允许创建新文件夹
        End With

        ' 显示对话框并返回结果
        Return If(dialog.ShowDialog() = DialogResult.OK, dialog.SelectedPath, "")
    End Function
    '
    ' 初始化 ListView 控件
    Public Sub InitLogListView(ByVal ListView As ListView)
        ' 设置 ListView 控件的列
        With ListView
            .Columns.Clear()
            .Columns.Add("Time", 130, HorizontalAlignment.Left)
            .Columns.Add("Log", 488, HorizontalAlignment.Left)
            .View = View.Details
        End With
    End Sub

    ''' <summary>
    ''' 计算金额范围ID
    ''' </summary>
    Public Function CalculateAmountRange(amount As Decimal) As Integer
        Select Case amount
            Case >= 1000000D : Return 100
            Case < 0 : Throw New ArgumentException("金额不能为负数")
            Case Else : Return Math.Min(CInt(amount \ 10000D), 99)
        End Select
    End Function
End Module